package cash.cmp.personal_financial_system.utils.login_else;

import cash.cmp.personal_financial_system.utils.allEnum.AlipayEnum;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Slf4j
public class AlipayLoginUtil {

    //通过授权码换取令牌
    public static String getToken(String code) throws IOException, AlipayApiException {
        System.out.println(code);

        AlipayConfig alipayConfig = new AlipayConfig();
        //设置网关地址
        alipayConfig.setServerUrl(AlipayEnum.URL.getValue());
        //设置应用ID
        alipayConfig.setAppId(AlipayEnum.APPID.getValue());
        //设置应用私钥
        alipayConfig.setPrivateKey(AlipayEnum.PRIVATE_KEY.getValue());
        //设置请求格式，固定值json
        alipayConfig.setFormat(AlipayEnum.FORMAT.getValue());
        //设置字符集
        alipayConfig.setCharset(AlipayEnum.CHARSET.getValue() );
        //设置签名类型
        alipayConfig.setSignType( AlipayEnum.SIGN_TYPE.getValue());
        //设置支付宝公钥
        alipayConfig.setAlipayPublicKey(AlipayEnum.ALIPAY_PUBLIC_KEY.getValue());
        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.open.public.template.message.industry.modify

        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setGrantType("authorization_code");
        request.setCode(code);
        //        request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
        AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
        String accessToken;
        if(response.isSuccess()){
            System.out.println("获取令牌调用成功");
            accessToken = response.getAccessToken();
        } else {
            System.out.println("获取令牌调用失败");
            return null;
        }
        return accessToken;

    }

    //通过令牌获取用户信息

    public static Map<String,String> getUserInfo(String token) throws AlipayApiException {
        System.out.println(token);

        AlipayConfig alipayConfig = new AlipayConfig();
        //设置网关地址
        alipayConfig.setServerUrl(AlipayEnum.URL.getValue());
        //设置应用ID
        alipayConfig.setAppId(AlipayEnum.APPID.getValue());
        //设置应用私钥
        alipayConfig.setPrivateKey(AlipayEnum.PRIVATE_KEY.getValue());
        //设置请求格式，固定值json
        alipayConfig.setFormat(AlipayEnum.FORMAT.getValue());
        //设置字符集
        alipayConfig.setCharset(AlipayEnum.CHARSET.getValue() );
        //设置签名类型
        alipayConfig.setSignType( AlipayEnum.SIGN_TYPE.getValue());
        //设置支付宝公钥
        alipayConfig.setAlipayPublicKey(AlipayEnum.ALIPAY_PUBLIC_KEY.getValue());
        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
        AlipayUserInfoShareResponse response = alipayClient.execute(request,token);
        Map<String,String> map = new HashMap<>();
        if(response.isSuccess()){
            System.out.println("获取用户信息调用成功");

            map.put("userId",response.getUserId());
            map.put("userName",response.getNickName());//实际为支付宝用户的昵称,个人开发者权限不够获取不到用户名
            map.put("avatar",response.getAvatar());//头像地址
            map.put("city",response.getCity());
            map.put("province",response.getProvince());
            map.put("gender",response.getGender());
            map.put("msg","success");

        } else {
            System.out.println("获取用户信息调用失败");
            map.put("msg","获取用户信息调用失败");
        }
        return map;
    }
}

